const tagsView = {
  state: {
    visitedViewsObj: JSON.parse(sessionStorage.getItem('visitedViewsObj')) || {'ERP':[{"name":"Dashboard","path":"/dashboard","title":"首页","query":{},"meta":{"title":"首页","icon":"app"}}],'WMS':[{"name":"Dashboard","path":"/dashboard","title":"首页","query":{},"meta":{"title":"首页","icon":"app"}}],'MiNi':[{"name":"Dashboard","path":"/dashboard","title":"首页","query":{},"meta":{"title":"首页","icon":"app"}}]},
    cachedViewsObj: JSON.parse(sessionStorage.getItem('cachedViewsObj')) || {'ERP':["Dashboard"],'WMS':["Dashboard"],'MiNi':["Dashboard"]},
  },
  mutations: {
    ADD_VISITED_VIEWS: (state, viewObj) => {
      const view = viewObj.route;
      const routerType = viewObj.routerType;
      const index = state.visitedViewsObj[routerType].findIndex(v => v.path === view.path)
      if (view.path !== '/dashboard') {
        if (index !== -1) state.visitedViewsObj[routerType].splice(index, 1)
        state.visitedViewsObj[routerType].push({
          name: view.name,
          path: view.path,
          title: view.meta.title || 'no-name',
          query: view.query,
          meta: view.meta
        })
        if (!view.meta.noCache) {
          state.cachedViewsObj[routerType].push(view.name)
        }
        sessionStorage.setItem('visitedViewsObj', JSON.stringify(state.visitedViewsObj))
        sessionStorage.setItem('cachedViewsObj', JSON.stringify(state.cachedViewsObj))
      } else {
        if (index == -1) {
          state.visitedViewsObj[routerType].unshift({
            name: view.name,
            path: view.path,
            title: view.meta.title || 'no-name',
            query: view.query,
            meta: view.meta
          })
          if (!view.meta.noCache) {
            state.cachedViewsObj[routerType].unshift(view.name)
          }
          sessionStorage.setItem('visitedViewsObj', JSON.stringify(state.visitedViewsObj))
          sessionStorage.setItem('cachedViewsObj', JSON.stringify(state.cachedViewsObj))
        }
      }
    },
    DEL_VISITED_VIEWS: (state, viewObj) => {
      const view = viewObj.route;
      const routerType = viewObj.routerType;

      for (const [i, v] of state.visitedViewsObj[routerType].entries()) {
        if (v.path === view.path) {
          state.visitedViewsObj[routerType].splice(i, 1)
          break
        }
      }
      for (const i of state.cachedViewsObj[routerType]) {
        if (i === view.name) {
          const index = state.cachedViewsObj[routerType].indexOf(i)
          state.cachedViewsObj[routerType].splice(index, 1)
          break
        }
      }
      sessionStorage.setItem('visitedViewsObj', JSON.stringify(state.visitedViewsObj))
      sessionStorage.setItem('cachedViewsObj', JSON.stringify(state.cachedViewsObj))
    },
    // // 未使用
    // DEL_OTHERS_VIEWS: (state, viewObj) => {
    //   const view = viewObj.route;
    //   const routerType = viewObj.routerType;
    //   for (const [i, v] of state.visitedViewsObj[routerType].entries()) {
    //     if (v.path === view.path) {
    //       state.visitedViewsObj[routerType] = state.visitedViewsObj[routerType].slice(i, i + 1)
    //       break
    //     }
    //   }
    //   for (const i of state.cachedViewsObj[routerType]) {
    //     if (i === view.name) {
    //       const index = state.cachedViewsObj[routerType].indexOf(i)
    //       state.cachedViewsObj[routerType] = state.cachedViewsObj[routerType].slice(index, i + 1)
    //       break
    //     }
    //   }
    //   sessionStorage.setItem('visitedViewsObj', JSON.stringify(state.visitedViewsObj))
    //   sessionStorage.setItem('cachedViewsObj', JSON.stringify(state.cachedViewsObj))
    // },
    // // 未使用
    // DEL_LEFT_VIEWS: (state, viewObj) => {
    //   const view = viewObj.route;
    //   const routerType = viewObj.routerType;
    //   const visitedViewsIndex = state.visitedViewsObj[routerType].findIndex(v => v.path === view.path)
    //   const cachedViewsIndex = state.cachedViewsObj[routerType].findIndex(i => i === view.name)

    //   if (visitedViewsIndex !== -1) {
    //     state.visitedViewsObj[routerType] = state.visitedViewsObj[routerType].slice(visitedViewsIndex)
    //   }

    //   if (cachedViewsIndex !== -1) {
    //     state.cachedViewsObj[routerType] = state.cachedViewsObj[routerType].slice(cachedViewsIndex)
    //   }

    //   sessionStorage.setItem('visitedViewsObj', JSON.stringify(state.visitedViewsObj))
    //   sessionStorage.setItem('cachedViewsObj', JSON.stringify(state.cachedViewsObj))
    // },
    // // 未使用
    // DEL_RIGHT_VIEWS: (state, viewObj) => {
    //   const visitedViewsIndex = state.visitedViews.findIndex(v => v.path === view.path)
    //   const cachedViewsIndex = state.cachedViews.findIndex(i => i === view.name)

    //   if (visitedViewsIndex !== -1) {
    //     state.visitedViews = state.visitedViews.slice(0, visitedViewsIndex + 1)
    //   }

    //   if (cachedViewsIndex !== -1) {
    //     state.cachedViews = state.cachedViews.slice(0, cachedViewsIndex + 1)
    //   }

    //   sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
    //   sessionStorage.setItem('cachedViews', JSON.stringify(state.cachedViews))
    // },
    DEL_ALL_VIEWS: (state, viewObj) => {
      const routerType = viewObj.routerType;
      state.visitedViewsObj[routerType] = []
      state.cachedViewsObj[routerType] = []
      sessionStorage.setItem('visitedViewsObj', JSON.stringify(state.visitedViewsObj))
      sessionStorage.setItem('cachedViewsObj', JSON.stringify(state.cachedViewsObj))
    },
    // // 未使用
    // CLONE_ALL_VIEWS: (state) => {
    //   console.log(state.visitedViews);
    //   state.oldVisitedViews = [...state.visitedViews]
    //   state.oldCachedViews = [...state.cachedViews]
    //   sessionStorage.setItem('oldVisitedViews', JSON.stringify(state.oldVisitedViews))
    //   sessionStorage.setItem('oldCachedViews', JSON.stringify(state.oldCachedViews))
    // },
    // // 未使用
    // PAST_ALL_VIEWS: (state, viewObj) => {
    //   state.visitedViews = [...viewObj.oldVisitedViews]
    //   state.cachedViews = [...viewObj.oldCachedViews]
    //   sessionStorage.setItem('visitedViews', JSON.stringify(state.visitedViews))
    //   sessionStorage.setItem('cachedViews', JSON.stringify(state.cachedViews))
    // }
  },
  actions: {
    addVisitedViews({ commit }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViewsObj[view.routerType]])
      })
    },
    // // 未使用
    // delOthersViews({ commit, state }, view) {
    //   return new Promise((resolve) => {
    //     commit('DEL_OTHERS_VIEWS', view)
    //     resolve([...state.visitedViews])
    //   })
    // },
    // // 未使用
    // delLeftViews({ commit, state }, view) {
    //   return new Promise((resolve) => {
    //     commit('DEL_LEFT_VIEWS', view)
    //     resolve([...state.visitedViews])
    //   })
    // },
    // // 未使用
    // delRightViews({ commit, state }, view) {
    //   return new Promise((resolve) => {
    //     commit('DEL_RIGHT_VIEWS', view)
    //     resolve([...state.visitedViews])
    //   })
    // },
    delAllViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_ALL_VIEWS',view)
        resolve([...state.visitedViewsObj[view.routerType]])
      })
    },
    // // 未使用
    // cloneAllViews({ commit }) {
    //   commit('CLONE_ALL_VIEWS')
    // },
    // // 未使用
    // pastAllViews({ commit }, viewObj) {
    //   commit('PAST_ALL_VIEWS', viewObj)
    // }
  }
}

export default tagsView
